<?php
/**
 * 节点控制器
 * @author zengnz
 *
 */
class NodeAction extends CommonAction
{
	public function index() {
		$nodeList = D('Node', 'Logic') -> getAllNodes();
		
 		$this -> assign('nodeList', $nodeList);
		$this -> display();
	}
	
	public function add() {
		if ($_POST) {
			$Model = D('Node');
			if (false === $Model -> create ()) {
				$this -> error ( $Model -> getError () );
			}
			if ( $Model -> pid < 0 ) 
				$this -> error("参数错误！");
			if ( !$Model -> title) 
				$this -> error('类别名称必填！');
			
			$errMsg = '已存在相同的一级类别';
			if ($Model -> pid)
				$errMsg = '该类别下已存在相同的子类别名称';
				
			$record = D('Node') -> where('pid='.$Model -> pid.' AND title="'.$Model -> title.'"') -> find();
			if ($record) {
				$this -> error($errMsg);
			}
			
			$pid = $Model -> pid;
			$sort = $Model -> sort;
			$title = $Model -> title;
			$Model -> path = ucfirst( $Model -> path );
			$ret = $Model -> add();
			
			if ($ret) {
				$sql = 'id != ' . $ret . ' AND pid = ' . $pid . ' AND sort = ' . $sort;
				$record = D('Node') -> where($sql) -> find();
				if ($record)
					$this -> reOrder($ret, $pid, $sort);
				D('Log') -> actionLog(LogModel::ADD, '添加类别，类别名称：'.$title);
				
				$this -> success('添加类别成功！');
			} else {
				$this -> error('添加类别失败！');	
			}	
			
		} else {
			$pid = intval($this -> _get('pid'));
			$pname = '一级类别';
			if ($pid) {
				$record = D('Node') -> where('id='.$pid) -> find();
				$pname = $record['title'];
			}
			
			$this -> assign('pname', $pname);
			$this -> assign('pid', $pid);
			$this -> display();
		}		
	}
	
	public function update() {
		if ($_POST) {
			$id = intval($this -> _post('id'));
			$pid = intval($this -> _post('pid'));
			$path = ucfirst( trim($this -> _post('path')) );
			$sort = intval($this -> _post('sort'));
			$title = trim($this -> _post('title'));
			
			$sql = 'id != ' . $id . ' AND pid = ' . $pid . ' AND title = "' . $title . '"';
			$record = D('Node') -> where($sql) -> find();
			if ($record) 
				$this -> error('该父级类别下已存在相同的类别名称！');
			
			if ($pid) {
				$sql = 'id != ' . $id . ' AND pid = '. $pid .' AND path = "' . $path . '"';
				$record = D('Node') -> where($sql) -> find();
				if ($record)
					$this -> error('该父级类别下已存在相同的路径名称！');
			}
			
			$data = array(
				'path' => $path,
				'title' => $title,
				'sort' => $sort						
			);
			
			$ret = D('Node') -> where('id = ' . $id) -> save($data);
			
			if ($ret) {
				$sql = 'id != ' . $id . ' AND pid = ' . $pid . ' AND sort = ' . $sort;
				$record = D('Node') -> where($sql) -> find();
				if ($record)
					$this -> reOrder($id, $pid, $sort);
				D('Log') -> actionLog(LogModel::UPDATE, '更新类别，类别ID：'.$id.'，类别名称：'.$title);
				
				$this -> success('修改类别成功！');
			} else {
				$this -> error('修改类别失败！');
			}
		} else {
			$id = intval( $this -> _get('id') );
			$record = D('Node') -> where('id = ' . $id) -> find();
			$pTitle = '一级类别';
			if ($record && $record['pid'] != 0) {
				$sql = 'id = ' . $record['pid'];
				$parentRecord = D('Node') -> where($sql) -> find();
				$pTitle = $parentRecord['title'];
			}
			
			$this -> assign('pTitle', $pTitle);
			$this -> assign('record', $record);
			$this -> display();
		}
	}

	
	/**
	 * 重新排序
	 * @param integer $pid
	 * @param integer $sort
	 * @return unknown
	 */
	protected function reOrder($id, $pid, $sort) {
		$sql = 'id != ' . $id . ' AND pid = ' . $pid . ' AND sort >= ' . $sort;
		$records = D('Node') -> where($sql) -> order('sort ASC') -> select();
		if ($records) {
			foreach ($records as $key => $record) {
				$sql = 'id = ' . $record['id'] . ' AND pid = ' . $record['pid'];
				$data['sort'] = ++$sort;
				$ret = D('Node') -> where($sql) -> save($data);
			}
		}
	}
	
}